package logic

import (
	"context"
	"eight-dimensional-mall/goods_srv/goods"

	"eight-dimensional-mall/api/internal/svc"
	"eight-dimensional-mall/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddGoodsLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddGoodsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddGoodsLogic {
	return &AddGoodsLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *AddGoodsLogic) AddGoods(req *types.AddGoodsRequest) (resp *types.Response, err error) {
	_, err = l.svcCtx.GoodsSrv.CreateGoods(l.ctx, &goods.CreateGoodsReq{
		GoodsTitle: req.GoodsTitle,
		GoodsPrice: float32(req.GoodsPrice),
		GoodsStock: req.GoodsStock,
		GoodsImg:   req.GoodsImg,
	})
	if err != nil {
		return &types.Response{
			Code: -1,
			Msg:  "服务器内部错误",
			Data: nil,
		}, nil
	}

	return &types.Response{
		Code: 0,
		Msg:  "添加商品成功",
		Data: nil,
	}, nil
}
